﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="../../../Scripts/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="../../../Scripts/QueryString2016.js"></script>
    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" />
    <script src="../../../Scripts/config.js" type="text/javascript"></script>
    <script src="../../../Comm/Gener.js" type="text/javascript"></script>

    <script type="text/javascript">
        //页面启动函数.
        var initData = {};
        var pageData = {};

        $(function () {

            pageData = {
                FK_MapData: GetQueryString("FK_MapData"),
                KeyOfEn: GetQueryString("KeyOfEn"),
                SelectIntKey: GetQueryString("SelectIntKey"),
                MyPK: GetQueryString("MyPK")
            };

            if (pageData.selectIntKey == null)
                pageData.selectIntKey = "0";

            //设置是否启用?
            var pkval = GetQueryString("FK_MapData") + "_" + GetQueryString("KeyOfEn");
            var attr = new Entity("BP.Sys.MapAttr", pkval);
            document.getElementById("RB_IsEnableJS_0").checked = true;
            if (attr.AtPara.indexOf('@IsEnableJS=1') >= 0)
                document.getElementById("RB_IsEnableJS_1").checked = true;

            //获得的数据.
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Admin_FoolFormDesigner_MapExt");
            handler.AddUrlData();
            var data = handler.DoMethodReturnString("RadioBtns_Init");

            $('#lb_EnName').text(pageData.KeyOfEn);

            if (data != undefined && data.indexOf('err@') == 0) {
                console.error(data);
                return;
            }

            initData = JSON.parse(data);

            //绑定左侧的列表值
            var ulListValuesLiHtml = '';

            $.each(initData.Sys_FrmRB, function (i, rb) {
                ulListValuesLiHtml += '<li style="list-style-type:none;float:left;" >' + '<input id="RB_' + rb.KeyOfEn + rb.IntKey + '"' + (rb.IntKey == pageData.selectIntKey ? ' checked="true" ' : '') + '     type="radio" name="' + pageData.KeyOfEn + '" value="' + rb.IntKey + '" /> <label for="RB_' + rb.KeyOfEn + rb.IntKey + '" > ' + rb.Lab + '</label></li>';
            })
            $('#ulListValues').html(ulListValuesLiHtml);

            $('[name=' + pageData.KeyOfEn + ']').bind('change', function (obj) {
                SaveCurrentConfig();
                pageData.selectIntKey = $(obj.target).val();
                BindConfig();
            })

            //绑定表单的所有字段
            var mapAttrHtml = '';
            //            alert(initData.Sys_MapAttr);
            $.each(initData.Sys_MapAttr, function (i, attr) {

                mapAttrHtml += '<tr>';
                mapAttrHtml += '<td>' + (i + 1) + '</td>';
                mapAttrHtml += '<td>' + attr.KeyOfEn + '</td>';
                mapAttrHtml += '<td>' + attr.Name + '</td>';
                mapAttrHtml += '<td><input type="radio" id="' + attr.KeyOfEn + '0" name="' + attr.KeyOfEn + '" value="0"/><label for="' + attr.KeyOfEn + '0" >不设置</label></td>';
                mapAttrHtml += '<td><input type="radio" id="' + attr.KeyOfEn + '1" name="' + attr.KeyOfEn + '" value="1"/><label for="' + attr.KeyOfEn + '1" >可用</label></td>';
                mapAttrHtml += '<td><input type="radio" id="' + attr.KeyOfEn + '2" name="' + attr.KeyOfEn + '" value="2"/><label for="' + attr.KeyOfEn + '2" >可见</label></td>';
                mapAttrHtml += '<td><input type="radio" id="' + attr.KeyOfEn + '3" name="' + attr.KeyOfEn + '" value="3"/><label for="' + attr.KeyOfEn + '3" >不可见</label></td>';

                //设置的值.
                //判断是否枚举类型                           
                if (attr.MyDataType == 2 && attr.LGType == 1) {
                    var ses = new Entities("BP.Sys.SysEnums");
                    ses.Retrieve("EnumKey", attr.UIBindKey);
                    var operations = "<option  value=''><label>&nbsp;&nbsp;&nbsp;</label></option>";
                    $.each(ses, function (i, obj) {
                        operations += "<option  value='" + obj.IntKey + "'>" + obj.Lab + "</option>";

                    });
                    mapAttrHtml += "<td><div id='DIV_" + attr.KeyOfEn + "'><select class='form-control' name='DDL_" + attr.KeyOfEn + "' id='DDL_" + attr.KeyOfEn + "' style='width:140px'>" + operations + "</select></div></td>";
                } else {
                    mapAttrHtml += '<td><input type="text" id="TB_' + attr.KeyOfEn + '" name="TB_' + attr.KeyOfEn + '" value="" /></td>';
                }

                mapAttrHtml += '</tr>';

                //mapAttrHtml += '<tr><td>' + (i + 1) + '</td><td>' + attr.KeyOfEn + '</td><td>' + attr.Name + '</td><td><input type="radio" id="' + attr.KeyOfEn + '0" name="' + attr.KeyOfEn + '" value="0"/><label for="' + attr.KeyOfEn + '0" >不设置</label></td><td><input type="radio" id="' + attr.KeyOfEn + '1" name="' + attr.KeyOfEn + '" value="1"/><label for="' + attr.KeyOfEn + '1" >可用</label></td><td><input type="radio" id="' + attr.KeyOfEn + '2" name="' + attr.KeyOfEn + '" value="2"/><label for="' + attr.KeyOfEn + '2" >可见</label></td></td><td><input type="radio" id="' + attr.KeyOfEn + '3" name="' + attr.KeyOfEn + '" value="3"/> <label for="' + attr.KeyOfEn + '3" >不可见</label></td></tr>';

            })
            $('#Fields tbody').html(mapAttrHtml);

            //为表单字段绑定DATA
            $.each($('#Fields tbody tr'), function (i, tr) {
                $(tr).data(initData.Sys_MapAttr[i]);
            });

            BindConfig();
        });

        function bindData() {

            //保存切换配置前的

            //绑定切换后的配置
            BindConfigData();
        }

        //保存当前配置
        function SaveCurrentConfig() {

            var currentSaveRb = $.grep(initData.Sys_FrmRB, function (value) {
                return value.IntKey == pageData.selectIntKey;
            });

            var configData = $('#formFields').serialize();

            //中文序列化乱码
            configData = decodeURIComponent(configData, true);

            configDataArr = configData.split('&');

            configDataArr = $.grep(configDataArr, function (value) {

                return (value.split('=').length == 2 && value.split('=')[1].length > 0);

            });

            var fieldsCfg = "";
            var setValue = "";

            if (configDataArr.length > 0) {
                for (var i = 0; i < configDataArr.length; i++) {
                    if (configDataArr[i].indexOf("TB_") == -1 && configDataArr[i].indexOf("DDL_") == -1)
                        fieldsCfg += '@' + configDataArr[i];
                    else
                        if (configDataArr[i].indexOf("TB_") >= 0)
                            setValue += '@' + configDataArr[i].replace("TB_", "");
                        else
                            setValue += '@' + configDataArr[i].replace("DDL_", "");
                }

            }

            var script = $('#TB_Script').val();
            var tip = $('#TB_Tip').val();

            currentSaveRb[0].Script = script;
            currentSaveRb[0].FieldsCfg = fieldsCfg;
            currentSaveRb[0].Tip = tip;
            currentSaveRb[0].SetVal = setValue;

        }


        function SaveEditData() {
            var selectRb = $.grep(initData.Sys_FrmRB, function (value) {
                return value.IntKey == pageData.selectIntKey;
            });
            $('#TB_Script').val(selectRb.Script);
            $('#TB_Tip').val(selectRb.Tip);
        }

        //绑定右侧配置信息
        function BindConfig() {
            var selectRb = $.grep(initData.Sys_FrmRB, function (value) {
                return value.IntKey == pageData.selectIntKey;
            });
            $('#TB_Script').val(selectRb[0].Script);
            $('#TB_Tip').val(selectRb[0].Tip);
            //解析下面的元素显示与否
            var setVal = selectRb[0].SetVal;
            if (setVal != null && setVal != "") {
                var setVals = setVal.split("@");
                for (var i = 0; i < setVals.length; i++) {
                    if (setVals[i] != '' && setVals[i]) {
                        var valObjs = setVals[i].split('=');
                        if ($("#TB_" + valObjs[0]).length > 0)
                            $("#TB_" + valObjs[0]).val(valObjs[1]);
                        if ($("#DDL_" + valObjs[0]).length > 0)
                            $("#DDL_" + valObjs[0]).val(valObjs[1]);
                    }
                }

            }
            var fieldsCfg = selectRb[0].FieldsCfg;


            //'@ConfigInfo:' + $('[name=xx]:checked').val() + '@ViewSta:' + 
            //开始绑定字段状态信息
            if (fieldsCfg != undefined) {
                var viewStaStr = fieldsCfg;
                var viewStaArr = viewStaStr.split('@');
                var viewStaObj = {};
                $.each(viewStaArr, function (i, viewSta) {
                    if (viewSta != '' && viewSta)
                        viewStaObj[viewSta.split('=')[0]] = viewSta.split('=')[1];
                });
                //为表单字段绑定DATA
                $.each($('#Fields tbody tr'), function (i, tr) {
                    var keyOfEn = $(tr).data().KeyOfEn;
                    if (viewStaObj[keyOfEn] != undefined) {
                        $('#' + keyOfEn + viewStaObj[keyOfEn]).attr('checked', true);
                    } else {
                        $('#' + keyOfEn + '0').attr('checked', true);
                    }
                });
            } else {
                //为表单字段绑定DATA
                $.each($('#Fields tbody tr'), function (i, tr) {
                    var keyOfEn = $(tr).data().KeyOfEn;
                    $('#' + keyOfEn + '0').attr('checked', true);
                });
            }
        }

        //执行保存
        function Save() {

            //保存当前打开表单信息
            SaveCurrentConfig();

            $.ajax({
                data: { data: JSON.stringify(initData.Sys_FrmRB) },
                type: 'post',
                async: true,
                url: Handler + "?DoType=RadioBtns_Save&FK_MapData=" + pageData.FK_MapData + "&FK_MapExt=" + pageData.MyPK + "&KeyOfEn=" + pageData.KeyOfEn + "&m=" + Math.random(),
                dataType: 'html',
                success: function (data) {
                    alert(data)
                }
            });
        }

        function SaveAndClose() {
            Save();
            if (parent && parent.parent && typeof parent.parent.doCloseDialog === 'function') {
                parent.parent.doCloseDialog.call();
            } else {
                window.close();
            }
        }

        //设置是否启用
        function SaveEnableJS(ctrl) {

            var pkval = GetQueryString("FK_MapData") + "_" + GetQueryString("KeyOfEn");
            var attr = new Entity("BP.Sys.MapAttr", pkval);
            attr.SetPara("IsEnableJS", ctrl.value);
            attr.Update();

        }

        function Close() {
            if (parent && parent.parent && typeof parent.parent.doCloseDialog === 'function') {
                parent.parent.doCloseDialog.call();
            } else {
                window.close();
            }
        }
    </script>
</head>
<body>
    <table style="width:100%;">
        <caption><label id="lb_EnName"></label>字段的高级设置</caption>
        <tr>
            
            <td valign="top">

              <fieldset>
                    <legend>是否启用？</legend>

                      <input type="radio" value="1" id="RB_IsEnableJS_1" onclick="SaveEnableJS(this)" name="RB_IsEnableJS" /> <label for="RB_IsEnableJS_1">启用 </label>
                      <input type="radio" value="0" id="RB_IsEnableJS_0"  onclick="SaveEnableJS(this)"  name="RB_IsEnableJS" /> <label for="RB_IsEnableJS_0">禁用 </label>
                    
                </fieldset>

           
            <fieldset>
                    <legend> 列表值 </legend>
                    <ul id="ulListValues" style = "margin: 0;border: 0;padding: 0;">
                        
                    </ul>
                </fieldset>

                 

                <fieldset>
                    <legend>执行JS脚本</legend>
                    <div id="JS">
                        <textarea rows="3" cols="50" id="TB_Script"></textarea>
                    </div>
                </fieldset>
                <fieldset>
                    <legend>Tip 提示信息</legend>
                    <div id="Tip">
                        <textarea rows="3" cols="50" id="TB_Tip"></textarea>
                    </div>
                </fieldset>
                <fieldset>
                    <legend>联动其他的控件使其属性该表(可见，只读)</legend>
                    <form id="formFields">
                        <div id="Fields">
                            <table>
                                <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>字段</th>
                                        <th>字段名</th>
                                        <th>不设置</th>
                                        <th>可以编辑</th>
                                        <th>可见？</th>
                                        <th>不可见</th>
                                        <th>设置值</th>
                                    </tr>
                                </thead>
                                <tbody></tbody>
                            </table>
                        </div>
                    </form>
                </fieldset>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="button" ID="Btn_Save" value="保存" onclick="Save()" />
                <input type="button" value="保存并关闭" onclick="SaveAndClose()" />
                <input type="button" value="关闭" onclick="Close()"/>
            </td>
        </tr>
    </table>
</body>
</html>
